package ext.tianma.soap.conf;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import wt.util.WTProperties;

import com.ptc.core.logging.Log;
import com.ptc.core.logging.LogFactory;

public class MappingConfig {
	
	public static String Codebase = null;
	private static Document confDoc = null;
	private static Log log = LogFactory.getLog(MappingConfig.class);
	private static HashMap ConfigMap = new HashMap();
	
	static{
		SAXReader reader = new SAXReader();
		WTProperties props;
		try {
			props = WTProperties.getLocalProperties();
			Codebase = props.getProperty("wt.home")+File.separator+"codebase"+File.separator;
//			Codebase = "D:/workspace/Airchina-ACM/codebase/";
			confDoc = reader.read(Codebase + "conf"+File.separator + "SAPMappingConfig.xml");
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void init(){
		Element root = confDoc.getRootElement();
		List maps = root.selectNodes("MapEntity");
		for(int i=0;i<maps.size();i++){
			Element map = (Element)maps.get(i);
			String key = map.attributeValue("key");
			List sets = map.selectNodes("set");
			Iterator it = sets.iterator();
			HashMap setMap = new HashMap();
			while(it.hasNext()){
				Element set = (Element)it.next();
				String from = set.attributeValue("from");
				String to = set.attributeValue("to");
				setMap.put(from,to);
			}
			ConfigMap.put( key,setMap);
		}
	}
	public static void main(String[] args){
		String s = MappingConfig.getMappingValue("CAMPModel","B737-600/700/800");
//		System.out.println(s);
	}
	
	public static String getMappingValue(String key,String value){
		if(!ConfigMap.containsKey( key )){
			return value;
		}
		HashMap setmap = (HashMap)ConfigMap.get(key);
		if(!setmap.containsKey(value)){
			return value;
		}else{
			return (String)setmap.get(value);
		}
	}
}
